// 加法
export function numberAdd(arg1, arg2) {
  if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
    console.log("Error: 加法需要传入2个数字");
    return;
  }
  let arg1Length = 0; // a的小数部分长度
  let arg2Length = 0; // b的小数部分长度
  try {
    arg1Length = arg1.toString().split(".")[1].length;
  } catch (f) {}
  try {
    arg2Length = arg2.toString().split(".")[1].length;
  } catch (f) {}

  let resNum = 10 ** Math.max(arg1Length, arg2Length); // 保证a、b为整数的最小10次幂
  return (arg1 * resNum + arg2 * resNum) / resNum;
}

// 减法
export function numberSub(arg1, arg2) {
  if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
    console.log("Error: 减法运算需要传入2个数字");
    return;
  }
  let arg1Length = 0; // a的小数部分长度
  let arg2Length = 0; // b的小数部分长度
  try {
    arg1Length = arg1.toString().split(".")[1].length;
  } catch (f) {}
  try {
    arg2Length = arg2.toString().split(".")[1].length;
  } catch (f) {}

  let resNum = 10 ** Math.max(arg1Length, arg2Length); //保证a、b为整数的最小10次幂
  return (arg1 * resNum - arg2 * resNum) / resNum;
}

// 乘法
export function numberMul(arg1, arg2) {
  if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
    console.log("Error: 乘法运算需要传入2个数字");
    return;
  }
  let arg1Length = 0; // a的小数部分长度
  let arg2Length = 0; // b的小数部分长度
  try {
    arg1Length = arg1.toString().split(".")[1].length;
  } catch (f) {}
  try {
    arg2Length = arg2.toString().split(".")[1].length;
  } catch (f) {}

  return (
    (Number(arg1.toString().replace(".", "")) *
      Number(arg2.toString().replace(".", ""))) /
    10 ** (arg1Length + arg2Length)
  );
}

// 除法
export function numberDiv(arg1, arg2) {
  if ((!arg1 || !arg2) && arg1 != 0) {
    console.log("Error: 减法运算需要传入2个数字");
    return;
  }
  let arg1Length = 0; // a的小数部分长度
  let arg2Length = 0; // b的小数部分长度
  try {
    arg1Length = arg1.toString().split(".")[1].length;
  } catch (f) {}
  try {
    arg2Length = arg2.toString().split(".")[1].length;
  } catch (f) {}

  const molecule =
    Number(arg1.toString().replace(".", "")) * 10 ** (arg1Length + arg2Length);
  const denominator =
    Number(arg2.toString().replace(".", "")) * 10 ** (arg1Length + arg2Length);
  return molecule / denominator / 10 ** (arg1Length - arg2Length);
}

//处理三级分类
export const organizeArray = (items) => {
  // 根据tier分类
  const tier0 = items.filter((item) => item.tier === 0);
  const tier1 = items.filter((item) => item.tier === 1);
  const tier2 = items.filter((item) => item.tier === 2);
  // 初始化一级数组
  const result = tier0.map((item) => ({
    ...item,
    children: [],
  }));
  //父级id跟子级parentId进行关联
  tier1.forEach((item) => {
    const parent = result.find((p) => p.id === item.parentId);
    if (parent) {
      parent.children.push({
        ...item,
        subchildren: [],
      });
    }
  });
  tier2.forEach((item) => {
    const parent = result.find((p) =>
      p.children.some((c) => c.id === item.parentId)
    );
    if (parent) {
      const child = parent.children.find((c) => c.id === item.parentId);
      if (child) {
        child.subchildren.push(item);
      }
    }
  });
  return result;
};

export const handlePhone = (phone) => {
  var mobileRegex = /^1[3-9]\d{9}$/;
  if (mobileRegex.test(phone)) {
    return true;
  } else {
    return false;
  }
};

// 处理时间
export const formatDate = (params) => {
  let date = new Date(params);
  let y = date.getFullYear();
  let m = String(date.getMonth() + 1).padStart(2, "0");
  let d = String(date.getDate()).padStart(2, "0");
  let hours = String(date.getHours()).padStart(2, "0");
  let minutes = String(date.getMinutes()).padStart(2, "0");
  let seconds = String(date.getSeconds()).padStart(2, "0");
  return `${y}-${m}-${d} ${hours}:${minutes}:${seconds}`;
};

// 枚举值
export const filterEnum = (value, enumData) => {
  let filterData = {};
  if (Array.isArray(enumData)) {
    filterData = enumData.find((item) => item.value == value);
  }
  return filterData ? filterData.name : "";
};

/**
 * 大数字转k
 * @param bigNumber
 * @returns
 */
export const formatBigNumber = (bigNumber) => {
  if (bigNumber >= 1e9) {
    return `${(bigNumber / 1e9).toFixed(1)}B`; // 十亿及以上用B表示
  } else if (bigNumber >= 1e6) {
    return `${(bigNumber / 1e6).toFixed(1)}M`; // 百万及以上用M表示
  } else if (bigNumber >= 1e3) {
    return `${(bigNumber / 1e3).toFixed(1)}K`; // 千及以上用K表示
  } else {
    return bigNumber.toString(); // 小于千的直接用数字表示
  }
};

/**
 * 获取二维码参数
 */
export const getQueryString = (url, name) => {
  let reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
  let r = url.substr(1).match(reg);
  if (r != null) {
    return r[2];
  }
  return null;
};
